package com.user.controller;

import com.sky.constant.JwtClaimsConstant;



import com.sky.properties.JwtProperties;
import com.sky.result.Result;

import com.sky.utils.JwtUtil;

import com.user.pojo.dto.UserLoginDTO;
import com.user.pojo.entity.User;
import com.user.pojo.vo.UserLoginVO;
import com.user.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.HashMap;
import java.util.Map;

@RestController
@Slf4j
@RequestMapping("user/user")
@Api(tags="用户相关接口")
public class UserController {

      @Autowired
      UserService userService;
      @Autowired
      private JwtProperties jwtProperties;

      @PostMapping("login")
      @ApiOperation("微信登录")
      public Result<UserLoginVO> login(@RequestBody UserLoginDTO userLoginDTO) {
             log.info("授权码：{}", userLoginDTO.toString());
             //微信登录查询openid 和 id
             User user =userService.wxLogin(userLoginDTO);
             //生成jwt令牌
             Map<String,Object> claims = new HashMap<>();
             claims.put(JwtClaimsConstant.USER_ID,user.getId());
             String token= JwtUtil.createJWT(jwtProperties.getUserSecretKey(), jwtProperties.getUserTtl(), claims);
             UserLoginVO userLoginVO=UserLoginVO.builder()
                     .id(user.getId())
                     .openid(user.getOpenid())
                     .token(token)
                     .build();

             return Result.success(userLoginVO);
      }

    /**
     *根据map进行统计
     * @param
     * @return
     */
      @GetMapping("countUserMap")
      @ApiOperation("统计用户数量")
      public Result<Integer> countUserByMap(@RequestParam Map<String,Object> map) {
          return userService.countUserByMap(map);
      }

}
